﻿using System.IO;
using System.Text;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

namespace UFrame
{
    public class UFrameCodeWindow : Editor
    {
        [MenuItem("Plateface/UFrameCode/CreateAsmdef")]
        public static void CreateAsmdef()
        {
            string[] list = null;
            AssemblyDefineJson a = new AssemblyDefineJson();
            string json = string.Empty;

            list = Directory.GetDirectories(Application.dataPath, "UCodeEditor", SearchOption.AllDirectories);
            a.name = "UFrameCodeEditor";
            a.references = new[]
            {
                "UFrameCode",
                "Unity.TextMeshPro"
            };
            a.optionalUnityReferences = new string[0];
            a.includePlatforms = new[] { "Editor" };
            a.excludePlatforms = new string[0];
            a.allowUnsafeCode = false;
            a.overrideReferences = false;
            a.precompiledReferences = new string[0];
            a.autoReferenced = true;
            a.defineConstraints = new string[0];
            json = JsonMa.ToJson(a);
            File.WriteAllText(list[0] + "\\UFrameCodeEditor.asmdef", json, Encoding.UTF8);


            list = Directory.GetDirectories(Application.dataPath, "UCode", SearchOption.AllDirectories);
            a = new AssemblyDefineJson();
            json = string.Empty;
            a.name = "UFrameCode";
            a.references = new[]
            {
                "Unity.TextMeshPro"
            };
            a.optionalUnityReferences = new string[0];
            a.includePlatforms = new string[0];
            a.excludePlatforms = new string[0];
            a.allowUnsafeCode = false;
            a.overrideReferences = false;
            a.precompiledReferences = new string[0];
            a.autoReferenced = true;
            a.defineConstraints = new string[0];
            json = JsonMa.ToJson(a);
            File.WriteAllText(list[0] + "\\UFrameCode.asmdef", json, Encoding.UTF8);

            list = Directory.GetDirectories(Application.dataPath, "UCodeTools", SearchOption.AllDirectories);
            a = new AssemblyDefineJson();
            json = string.Empty;
            a.name = "UFrameCodeTool";
            a.references = new[]
            {
                "Unity.TextMeshPro",
                "UFrameCode",
            };
            a.optionalUnityReferences = new string[0];
            a.includePlatforms = new string[0];
            a.excludePlatforms = new string[0];
            a.allowUnsafeCode = false;
            a.overrideReferences = false;
            a.precompiledReferences = new string[0];
            a.autoReferenced = true;
            a.defineConstraints = new string[0];
            json = JsonMa.ToJson(a);
            File.WriteAllText(list[0] + "\\UFrameCodeTool.asmdef", json, Encoding.UTF8);

            list = Directory.GetDirectories(Application.dataPath, "UToolsEditor", SearchOption.AllDirectories);
            a = new AssemblyDefineJson();
            json = string.Empty;
            a.name = "UFrameCodeToolEditor";
            a.references = new[]
            {
                "Unity.TextMeshPro.Editor",
                "UFrameCode",
                "UFrameCodeTool",
                "UFrameCodeEditor",
            };
            a.optionalUnityReferences = new string[0];
            a.includePlatforms = new[] { "Editor" };
            a.excludePlatforms = new string[0];
            a.allowUnsafeCode = false;
            a.overrideReferences = false;
            a.precompiledReferences = new string[0];
            a.autoReferenced = true;
            a.defineConstraints = new string[0];
            json = JsonMa.ToJson(a);
            File.WriteAllText(list[0] + "\\UFrameCodeToolEditor.asmdef", json, Encoding.UTF8);

            AssetDatabase.Refresh();
        }

        [MenuItem("Plateface/UFrameCode/RemoveAsmdef")]
        public static void RemoveAsmdef()
        {
            RemoveFile(Application.dataPath, "UFrameCodeEditor.asmdef");
            RemoveFile(Application.dataPath, "UFrameCode.asmdef");
            RemoveFile(Application.dataPath, "UFrameCodeTool.asmdef");
            RemoveFile(Application.dataPath, "UFrameCodeToolEditor.asmdef");

            AssetDatabase.Refresh();
        }

        private static void RemoveFile(string path, string fileName)
        {
            var list = Directory.GetFiles(path, fileName, SearchOption.AllDirectories);
            if (list.Length > 0)
            {
                File.Delete(list[0]);
            }
        }


        /// <summary>
        /// 只用于UFrame Editor 的参数
        /// </summary>
        [DidReloadScripts]
        public static void LoadAssetConfig()
        {
            //Debug.Log("加载配置....");
            TextAsset t = Resources.Load<TextAsset>("UFrameAssetEditor");
            if (t == null)
            {
                return;
            }

            var lines = t.text.Split('\n');

            string path = string.Empty;

            //path = StringTool.LineByKeyPair("PlayerPrefsPath", lines);
            //if(string.IsNullOrEmpty(path))
            //PlayerPrefsCodeGenerate.classPath = path;

        }



    }

    public class AssemblyDefineJson
    {
        public string name;
        public string[] references;
        public string[] includePlatforms;
        public string[] excludePlatforms;
        public bool allowUnsafeCode;
        public bool autoReferenced;
        public bool overrideReferences;
        public string[] precompiledReferences;
        public string[] defineConstraints;
        public string[] optionalUnityReferences;
    }
}